//
//  YKRequestExchangeViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/10/4.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKRequestExchangeViewController.h"
#import "YKInputView.h"
#import "YKInputMultilineView.h"
@interface YKRequestExchangeViewController ()
@property (nonatomic, strong) NSString *apply_code;
@property (nonatomic, strong) NSString *apply_date;
@property (nonatomic, strong) NSString *achievementIntegral;//积分余额
@property (nonatomic, strong) YKInputMultilineView *exchangeContentView;
@property (nonatomic, strong)YKInputView *scoreView;
@end

@implementation YKRequestExchangeViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"申请兑换";
    // Do any additional setup after loading the view.
    [self tf_showStateView:kTFViewStateLoading];
    [TFHTTPMANAER POST:@"personalCentr/toAddApplyExchangeIntegral.htm" parameters:@{} success:^(id responseObject) {
        [self tf_showStateView:kTFViewStateNone];
        self.apply_code = [responseObject valueForKeyPath:@"apply_code"];
        self.apply_date = [responseObject valueForKeyPath:@"apply_date"];
        self.achievementIntegral = [NSString stringWithFormat:@"%@",[responseObject valueForKeyPath:@"achievementIntegral"]];
        [self updateViews];
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError];
    }];
}

- (void)updateViews {
    YKInputView *requestNoView = [[YKInputView alloc]initWithTitle:@"申请单号" input:self.apply_code];
    requestNoView.editable = NO;
    [self.view addSubview:requestNoView];
    
    YKInputView *leftScoreView = [[YKInputView alloc]initWithTitle:@"积分余额" input:self.achievementIntegral];
    leftScoreView.editable = NO;
    [self.view addSubview:leftScoreView];
    
    YKInputView *scoreView = [[YKInputView alloc]initWithTitle:@"积分兑换" input:@""];
    scoreView.keyboardType = UIKeyboardTypeDecimalPad;
    [self.view addSubview:scoreView];
    self.scoreView = scoreView;
    YKInputMultilineView *exchangeContentView = [[YKInputMultilineView alloc]initWithTitle:@"兑换内容" input:@""];
    [self.view addSubview:exchangeContentView];
    self.exchangeContentView = exchangeContentView;
    [self.view.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view.mas_leading).offset(13);
        make.trailing.equalTo(self.view.mas_trailing).offset(-13);
    }];
    [self.view.subviews mas_alignAlongAxis:MASAxisTypeVertical fixedSpace:@8 leadSpace:11];
    
    UIButton* saveButton = [[UIButton alloc]init];
    [self.view addSubview:saveButton];
    UIButton *cancleButton = [[UIButton alloc]init];
    [self.view addSubview:cancleButton];
    UIButton *submitButton = [[UIButton alloc]init];
    [self.view addSubview:submitButton];
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [submitButton setTitle:@"提交审批" forState:UIControlStateNormal];
    [saveButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [cancleButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [submitButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [saveButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [cancleButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [submitButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    saveButton.titleLabel.font = [UIFont systemFontOfSize:14];
    cancleButton.titleLabel.font = [UIFont systemFontOfSize:14];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [saveButton addTarget:self action:@selector(onSaveButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [cancleButton addTarget:self action:@selector(onCancleButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton addTarget:self action:@selector(onSubmitButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    [@[saveButton,cancleButton, submitButton] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:1 leadSpacing:0 tailSpacing:0];
    [@[saveButton,cancleButton, submitButton] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
}

- (void)onSaveButtonTapped:(id)sender {
    NSString *apply_code = self.apply_code;
    NSString *exchange_point = self.scoreView.input;
    NSString *exchange_content = self.exchangeContentView.input;
    if (self.scoreView.input.doubleValue > self.achievementIntegral.doubleValue) {
        [self showHint:@"兑换积分不能大于剩余积分"];
        return;
    }
    if ([exchange_content stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length == 0) {
        [self showHint:@"请输入兑换内容"];
        return;
    }
    [TFHTTPMANAER POST:@"personalCentr/doAddApplyExchangeIntegral.htm" parameters:@{@"apply_code":apply_code, @"exchange_point":exchange_point,@"exchange_content":exchange_content,@"subState":@"0"} HUD:@"提交中..." success:^(id responseObject) {
        [self.navigationController popViewControllerAnimated:YES];
        [self showHint:@"提交成功"];
    } failure:^(id responseObject, NSError *error) {
        [self showHint:@"提交失败, 请点击重试"];
    }];
}

- (void)onCancleButtonTapped:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onSubmitButtonTapped:(id)sender {
    
}
@end
